<template>
	<view>
		<u-sticky><u-tabs :list="list" :is-scroll="false" :current="current" @change="sectionChange"></u-tabs></u-sticky>
		<view class="content wrap" v-if="current == 0">
			<u-form :model="form2" ref="uForm"  :rules="rules">
				<u-form-item label="包装日期*" label-width="180" left-icon="calendar"><u-input v-model="form2.times" placeholder="出库日期" disabled="true" disabledColor="#b9babc"/></u-form-item>
				
				<u-form-item label="包装人编码" label-width="200" left-icon="account">
					<u-input v-model="form2.userNumber" placeholder="请输入包装人编码"/>
					<u-icon @click="scan()" slot="right" name="scan" size="60"></u-icon>
				</u-form-item>
				
				<u-form-item label="商品*" label-width="150">
					<u-radio-group v-model="form2.goodsName">
						<u-radio 
							@change="radioChange2" 
							v-for="(item, index) in glist" :key="index" 
							:name="item.name"
						>
						{{item.name}}
						</u-radio>
					</u-radio-group>
				</u-form-item>
				
				<u-form-item label="商品规格*" label-width="150">
					<u-radio-group v-model="form2.v1">
						<u-radio 
							@change="radioChange3" 
							v-for="(item, index) in gilist" :key="index" 
							:name="item.name"
						>
						{{item.name}}
						</u-radio>
					</u-radio-group>
				</u-form-item>
				
				<u-form-item label="毛重*" label-width="180"><u-input v-model="form2.gweight" type="digit" disabled="true" placeholder="自动计算"/></u-form-item>
				
				<u-form-item label="净重*" label-width="180"><u-input v-model="form2.nweight" type="digit" disabled="true" placeholder="自动计算"/></u-form-item>
				
				<u-form-item label="数量*" label-width="180" prop="nums"><u-input v-model="form2.nums" type="digit" placeholder="请输入数量" @input="numsinput"/></u-form-item>
				
				<u-form-item label="库位" label-width="180"><u-input v-model="form2.positionNumber" type="digit" placeholder="请输入库位"/></u-form-item>
			</u-form>
			<u-button @click="submit2">提交</u-button>
			
			<u-card padding="16">
				<view class="" slot="head">
					<view class="wrap">
						<u-row>
							<u-col span="12" text-align="center">
								<view class="weikaishi3">
									<u-icon name="search" size="30" labelSize="15px" labelColor="#000000" label="库存信息"></u-icon>
								</view>
							</u-col>
						</u-row>
					</view>
				</view>
				<view class="" slot="body">
					<view class="wrap">
						<u-row>
							<u-col span="2" text-align="center">
								<view class="weikaishi4 u-line-1 u-m-t-10">
									<span>仓库</span>
								</view>
							</u-col>
							<u-col span="2" text-align="center">
								<view class="weikaishi4 u-line-1 u-m-t-10">
									<span>商品</span>
								</view>
							</u-col>
							<u-col span="2" text-align="center">
								<view class="weikaishi4 u-line-1 u-m-t-10">
									<span>毛重</span>
								</view>
							</u-col>
							<u-col span="2" text-align="center">
								<view class="weikaishi4 u-line-1 u-m-t-10">
									<span>净重</span>
								</view>
							</u-col>
							<u-col span="2" text-align="center">
								<view class="weikaishi4 u-line-1 u-m-t-10">
									<span>箱数</span>
								</view>
							</u-col>
							<u-col span="2" text-align="center">
								<view class="weikaishi4 u-line-1 u-m-t-10">
									<span>日期</span>
								</view>
							</u-col>
						</u-row>
						<u-row v-for="(item,index) in dataList" :key="index">
							<u-col span="2" text-align="center">
								<view class=" u-m-t-10 weikaishi6 jinxingzhong">
									<span>{{item.warehouseName}}{{item.positionNumber}}</span>
								</view>
							</u-col>
							<u-col span="2" text-align="center">
								<view class=" u-m-t-10 weikaishi6 jinxingzhong">
									<span>{{item.goodsName}}{{item.grades}}-{{item.caseWeightOne}}</span>
								</view>
							</u-col>
							<u-col span="2" text-align="center">
								<view class=" u-m-t-10 weikaishi6 jinxingzhong">
									<span>{{item.gweight}}</span>
								</view>
							</u-col>
							<u-col span="2" text-align="center">
								<view class=" u-m-t-10 weikaishi6 jinxingzhong">
									<span>{{item.weight}}</span>
								</view>
							</u-col>
							<u-col span="2" text-align="center">
								<view class=" u-m-t-10 weikaishi6 jinxingzhong">
									<span>{{item.nums}}</span>
								</view>
							</u-col>
							<u-col span="2" text-align="center">
								<view class=" u-m-t-10 weikaishi6 huanghang jinxingzhong">
									<span>{{item.producetime}}</span>
								</view>
							</u-col>
						</u-row>
					</view>
				</view>
			</u-card>
		</view>
		<view>
			<u-toast ref="uToast" />
		</view>
		<view class="" v-if="current == 1">
			<u-card padding="16" v-for="(item,index) in dataList2" :key="index">
				<view class="" slot="head">
					<view class="wrap">
						<u-row>
							<u-col span="6" text-align="left">
								<view class="u-line-1 u-m-t-2 weikaishi3">
									<span>包装人编号：{{item.userNumber}}</span>
								</view>
							</u-col>
							<u-col span="6" text-align="left">
								<view class="u-line-1 u-m-t-2 weikaishi3">
									<span>状态：{{item.states}}</span>
								</view>
							</u-col>
						</u-row>
						<u-row>
							<u-col span="6" text-align="left">
								<view class="u-line-1 u-m-t-2 weikaishi">
									<span>时间：{{item.times}}</span>
								</view>
							</u-col>
							<u-col span="6" text-align="right">
								<view class="u-line-1 u-m-t-2 weikaishi">
									<span>商品：{{item.goodsName}}</span>
								</view>
							</u-col>
						</u-row>
					</view>
				</view>
				<view class="" slot="body">
					<view class="wrap">
						<u-row>
							<u-col span="6" text-align="left">
								<view class="u-line-1 u-m-t-4">
									<span>等级：</span><span>{{item.grades}}</span>
								</view>
							</u-col>
							<u-col span="6" text-align="left">
								<view class="u-line-1 u-m-t-4">
									<span>每箱重量：</span><span>{{item.caseWeightOne}}</span>
								</view>
							</u-col>
							<u-col span="6" text-align="left">
								<view class="u-line-1 u-m-t-4">
									<span>箱皮：</span><span>{{item.caseWeight}}</span>
								</view>
							</u-col>
							<u-col span="6" text-align="left">
								<view class="u-line-1 u-m-t-4">
									<span>毛重：</span><span>{{item.gweight}}</span>
								</view>
							</u-col>
							<u-col span="6" text-align="left">
								<view class="u-line-1 u-m-t-4">
									<span>净重：</span><span>{{item.nweight}}</span>
								</view>
							</u-col>
							<u-col span="6" text-align="left">
								<view class="u-line-1 u-m-t-4">
									<span>数量：</span><span>{{item.nums}}</span>
								</view>
							</u-col>
							<u-col span="6" text-align="left">
								<view class="u-m-t-4 huanhang">
									<span>生产日期：</span><span>{{item.producetime}}</span>
								</view>
							</u-col>
							<u-col span="6" text-align="left">
								<view class="u-line-1 u-m-t-4">
									<span>库位：</span><span>{{item.positionNumber}}</span>
								</view>
							</u-col>
						</u-row>
					</view>
				</view>
				<view class="foot-butten" slot="foot" v-if="item.states == '已入'">
					<view :class="flex">
						<u-col span="12" text-align="center" @click="revoke(item.id)">
							<view class="u-m-10 yizuofei">
								撤销
							</view>
						</u-col>
					</view>
				</view>
			</u-card>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				flex:'',
				list: [
					{
						name: '成品包装'
					}, 
					{
						name: '包装查询'
					}
				],
				current: 0,
				dataList:[],
				dataList2: [],
			    pageNo: 1,
			    hasMore: true,
			    orderCount: 0,
				form2:{
				    times:'',
				    userNumber:"",
					goodsName:'',
					v1:'',
				    grades:"",
					caseWeightOne:"",
				    caseWeight:"",
				    gweight:"",
				    nweight:"",
				    nums:"",
				    positionNumber:"",
				},
				glist:[],
				gilist:[],
				gilistbasic:[],
			    rules: {
					nums: [
						{ 
							required: true,
							message: '请输入数量', 
							trigger: 'blur',
						}
					],
				}
			}
		},
		onPullDownRefresh() {
			this.refresh()
			uni.stopPullDownRefresh()
		},
		onReachBottom() {
			if(this.current == 1){
				uni.showLoading({
					title: '加载中'
				})
				if (this.hasMore) {
					this.pageNo++ 
					setTimeout(() => {
						this.loadOrder({
						}).then(res => {})
						uni.hideLoading()
					}, 500)
				} else {
					setTimeout(() => {
						uni.hideLoading()
						uni.showToast({
							icon: 'none',
							title: '已经到低了哦',
							duration: 2000
						});
					}, 500)
				}
			}
		},
		onLoad(open) {
			//#ifdef H5 || APP-PLUS
			this.flex = 'u-flex'
			//#endif
			this.refresh()
			
			let year = new Date().getFullYear();//年
			let month = new Date().getMonth() + 1;//注意！月份是从0月开始获取的，所以要+1;
			let day = new Date().getDate();//日
			this.form2.times =  year + '-' + (month >= 10 ? month : '0' + month) + '-' + (day >= 10 ? day : '0' + day);
			
			this.$http.post('/rest/fruits/findgoodslist',{
			},{
				params: {},
				header: {
					'Authorization' :uni.getStorageSync('token'),
				}
			}).then(res => {
				if(res.data.data.length>0){
					this.form2.goodsName = res.data.data[0].goodsName
					for(let item1 of res.data.data) {
						let obj = {}
						obj.name = item1.goodsName
						this.glist.push(obj)
					}
					this.$http.post('/rest/fruits/findgoodsInstalllist',{
					},{
						params: {goodsName:res.data.data[0].goodsName},
						header: {
							'Authorization' :uni.getStorageSync('token'),
						}
					}).then(res => {
						if(res.data.data.length>0){
							this.gilistbasic = res.data.data
							this.form2.v1 = res.data.data[0].v1
							this.form2.grades = res.data.data[0].grades
							this.form2.caseWeightOne = res.data.data[0].caseWeightOne
							this.form2.caseWeight = res.data.data[0].caseWeight
							for(let item1 of res.data.data) {
								let obj = {}
								obj.name = item1.v1
								this.gilist.push(obj)
							}
							
						}
					})
				}
			})
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		},
		methods: {
			revoke(id) {
				uni.showModal({
					content: '确定撤销吗？',
					success: (res) => {
						if (res.confirm) {
							uni.showLoading({
								title: '正在提交'
							})
							this.$http.post('/rest/fruits/revoke_packing',{
							},{
								params: {id:id},
								header: {
									'Authorization' :uni.getStorageSync('token'),
								}
							}).then(res => {
								uni.hideLoading()
								if(res.data.code == 200) {
									this.$refs.uToast.show({
										title: '操作成功',
										type: 'success'
									})
									this.refresh()
								} else {
									this.$refs.uToast.show({
										title: res.data.message,
										type: 'error'
									})
								}
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				})
			},
			numsinput(e){
				if(e == ''){
					this.form2.gweight = ''
					this.form2.nweight = ''
					return
				}
				if(typeof parseFloat(e) != 'number' ||  parseFloat(e) <= 0){
					uni.showToast({
						title: "请输入正确数值",
						icon: 'none'
					})
					return
				}
				this.form2.gweight  = (e*this.form2.caseWeightOne).toFixed(3)
				this.form2.nweight  = (e*this.form2.caseWeightOne)-(e*this.form2.caseWeight).toFixed(3)
			},
			scan(){
				let that = this
				uni.scanCode({
					success: function (res) {
						console.log('条码类型：' + res.scanType);
						console.log('条码内容：' + res.result);
						that.form2.userNumber = res.result
					}
				});
			},
			// 选中某个单选框时，由radio时触发
			radioChange2(e) {
				console.log(e);
				this.$http.post('/rest/fruits/findgoodsInstalllist',{
				},{
					params: {goodsName:e},
					header: {
						'Authorization' :uni.getStorageSync('token'),
					}
				}).then(res => {
					if(res.data.data.length>0){
						this.gilistbasic = res.data.data
						this.form2.v1 = res.data.data[0].v1
						this.form2.grades = res.data.data[0].grades
						this.form2.caseWeightOne = res.data.data[0].caseWeightOne
						this.form2.caseWeight = res.data.data[0].caseWeight
						this.form2.gweight  = ''
						this.form2.nweight  = ''
						this.form2.nums = ''
						for(let item1 of res.data.data) {
							let obj = {}
							obj.name = item1.v1
							this.gilist.push(obj)
						}
						
					}
				})
			},
			radioChange3(e) {
				console.log(e);
				for(let item1 of this.gilistbasic) {
					if(item1.v1 == e){
						this.form2.grades = item1.grades
						this.form2.caseWeightOne = item1.caseWeightOne
						this.form2.caseWeight = item1.caseWeight
						
						if(this.form2.nums != '' && this.form2.nums != null){
							if(typeof parseFloat(e) != 'number' ||  parseFloat(e) <= 0){
								return
							}
							this.form2.gweight  = (this.form2.nums*this.form2.caseWeightOne).toFixed(3)
							this.form2.nweight  = (this.form2.nums*this.form2.caseWeightOne)-(this.form2.nums*this.form2.caseWeight).toFixed(3)
						}
						return
					}
				}
			},
			showDetailed(outNumber){
				this.outNumber = outNumber
				this.$http.post('/rest/fruits/findzhifacangOutDetailedlist',{
				},{
					params: {outNumber:outNumber},
					header: {
						'Authorization' :uni.getStorageSync('token'),
					}
				}).then(res => {
					this.dataList3 = res.data.data
					this.popupShow = true
				})
			},
			sectionChange(e){
				this.current = e
				this.refresh()
			},
			submit2() {
				this.$refs.uForm.validate(valid => {
					if (valid) {
						console.log('验证通过');
						if(typeof this.form2.goodsName == 'undefined' || this.form2.goodsName == null || this.form2.goodsName == ""){
							uni.showToast({
								title: "请选择商品",
								icon: 'none'
							})
							return
						}
						if(typeof this.form2.v1 == 'undefined' || this.form2.v1 == null || this.form2.v1 == ""){
							uni.showToast({
								title: "请选择商品规格",
								icon: 'none'
							})
							return
						}
						if(typeof this.form2.gweight == 'undefined' || this.form2.gweight == null || this.form2.gweight == ""){
							uni.showToast({
								title: "请输入毛重",
								icon: 'none'
							})
							return
						}
						if(typeof this.form2.nweight == 'undefined' || this.form2.nweight == null || this.form2.nweight == ""){
							uni.showToast({
								title: "请输入净重",
								icon: 'none'
							})
							return
						}
						uni.showLoading({
							title: '正在提交'
						})
						this.$http.post('/rest/fruits/addItemFruitsPacking',{
						},{
							params: this.form2,
							header: {
								'Authorization' :uni.getStorageSync('token'),
							}
						}).then(res => {
							uni.hideLoading()
							if(res.data.code == 200) {
								this.form2.gweight = ''
								this.form2.nweight = ''
								this.form2.nums = ''
								this.$refs.uToast.show({
									title: '操作成功',
									type: 'success'
								})
								this.refresh()
							} else {
								this.$refs.uToast.show({
									title: res.data.message,
									type: 'error'
								})
							}
						})
					} else {
						console.log('验证失败');
					}
				});
			},
			refresh(){
				uni.showLoading({
					title: '加载中...'
				})
				if(this.current == 0){
					this.$http.post('/rest/fruits/findStorageStockSumBySpec3',{
					},{
						params: {
						},
						header: {
							'Authorization' :uni.getStorageSync('token'),
						}
					}).then(res => {
						this.dataList = res.data.data
					})
				} else if(this.current == 1){
					this.pageNo = 1
					this.hasMore = true
					this.loadOrder({
					}).then(res => {
					})
				}
				uni.hideLoading()
			},
			loadOrder(param = {}) {
				return new Promise((resolve, reject) => {
					param = Object.assign(param, {
						page: this.pageNo,
						limit: 10
					})
					this.$http.post('/rest/fruits/findPackingPagelist',{
					},{
						params: param,
						header: {
							'Authorization' :uni.getStorageSync('token'),
						}
					}).then(res => {
						if (this.pageNo == 1) {
							this.dataList2 = res.data.data
						} else {
							this.dataList2 = this.dataList2.concat(res.data.data)
						}
						this.orderCount = res.data.count
						this.hasMore = this.pageNo * 10 < res.data.count
						resolve(res)
					})
				})
			}
		}
	}
</script>
<style lang="scss">
	page{
			background-color: #ffffff;
	}
	
	.wrap {
		padding: 30rpx;
	}
	.weikaishi2{
		font-size: 36rpx;
	}
	.weikaishi3{
		font-size: 32rpx;
	}
	.huanhang{
		word-break:break-all;
	}
</style>
